using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using System.Linq.Expressions;
using ACME.Northwind.Service.Interfaces;

namespace ACME.Northwind.Service.IOCExtensions
{
	/// <summary>
	/// This is an extension class for Category DTO objects and lists
	/// </summary>
	public static partial class CategoryExtension
	{
		#region RunSelect
		/// <summary>
		/// Select all objects from store.
		/// </summary>
		public static void RunSelect(this List<CategoryDTO> list)
		{
			var proxy = ProxyFactory<CategoryDTO>.GetProxy();
			list.AddRange(proxy.RunSelect());
		}
		#endregion

		#region RunSelect Paged

		/// <summary>
		/// Using the specified Where expression, execute a query against the database to return a paged result set.
		/// </summary>
		public static PagedQueryResults<CategoryDTO> RunSelect(this List<CategoryDTO> list, int page, int pageSize, bool ascending, string sortColumn, string linq)
		{
			var proxy = ProxyFactory<CategoryDTO>.GetProxy();
			PagedQueryResults<CategoryDTO> retval = proxy.RunSelect(page, pageSize, ascending, sortColumn, linq);
			if (retval.GridData != null)
				list.AddRange(retval.GridData);
			return retval;
		}

		#endregion

		#region RunSelect Paged

		/// <summary>
		/// Using the specified Where expression, execute a query against the database to return a result set.
		/// </summary>
		public static void RunSelect(this List<CategoryDTO> list, string linq)
		{
			var proxy = ProxyFactory<CategoryDTO>.GetProxy();
			list.AddRange(proxy.RunSelect(linq));
		}

		#endregion

		#region Persist

		/// <summary>
		/// Persists this object to store.
		/// </summary>
		public static void Persist(this CategoryDTO category)
		{
			var proxy = ProxyFactory<CategoryDTO>.GetProxy();
			proxy.Persist(category);
		}

		/// <summary>
		/// Persists this collection to store.
		/// </summary>
		public static void Persist(this List<CategoryDTO> categoryList)
		{
			var proxy = ProxyFactory<CategoryDTO>.GetProxy();
			proxy.Persist(categoryList);
		}

		#endregion

		#region Category SelectUsingPK

		/// <summary>
		/// Select a single object from this collection by its primary key.
		/// </summary>
		public static bool SelectByPrimaryKey(this CategoryDTO category, int categoryid)
		{
			var proxy = ProxyFactory<CategoryDTO>.GetProxy();
			ICategoryService categoryService = (ICategoryService)proxy;
			var item = categoryService.SelectByPrimaryKey(categoryid);
			if (item == null) return false;
			CopyObject(item, category);
			return true;
		}

		#endregion

		#region Category Select By Foreign Key
		/// <summary>
		/// Get the related 'Product' DTO list
		/// </summary>
		public static ProductDTO[] GetProductList(this CategoryDTO item)
		{
			var proxy = ProxyFactory<CategoryDTO>.GetProxy();
			ICategoryService service = (ICategoryService)proxy;
			return service.GetProductList(item).ToArray();
		}


		#endregion

		#region Delete

		/// <summary>
		/// Delete a single object.
		/// </summary>
		public static void Delete(this CategoryDTO category)
		{
			var proxy = ProxyFactory<CategoryDTO>.GetProxy();
			ICategoryService categoryService = (ICategoryService)proxy;
			categoryService.Delete(category);
		}

		#endregion

		/// <summary>
		/// Clones one DTO to another.
		/// </summary>
		internal static void CopyObject(CategoryDTO source, CategoryDTO destination)
		{
			destination.CategoryName = source.CategoryName;
			destination.CategoryId = source.CategoryId;
			destination.Picture = source.Picture;
			destination.Description = source.Description;
		}

	}
}
